<template>
  <el-select v-model="sysCodeValueList" :placeholder="sysCodeName" multiple v-if="multiple">
    <el-option v-for="item in sysCodeList" :key="item.codeValue" :label="item.codeName" :value="item.codeValue"></el-option>
  </el-select>
  <el-select v-model="sysCodeValue" :placeholder="sysCodeName" clearable v-else>
    <el-option v-for="item in sysCodeList" :key="item.codeValue" :label="item.codeName" :value="item.codeValue"></el-option>
  </el-select>
</template>

<script>
  import {
    querySysCode
  } from '@/api/exp/sys-code'

  export default {
    name: 'ScSelect',
    props: {
      type: String,
      value: [String, Array]
    },
    methods: {
      init() {
        if (this.value instanceof Array) {
          this.multiple = true
          this.sysCodeValueList = this.value
        } else {
          this.multiple = false
          this.sysCodeValue = this.value
          console.log('value: ' + this.sysCodeValue)
        }
        this.querySysCode()
      },
      querySysCode() {
        querySysCode({
          condition: [{
              property: 'codeType',
              value: this.type,
              type: 'EQ'
            },
            {
              property: 'status',
              value: '0',
              type: 'EQ'
            }
          ],
          sort: [{
            property: 'orderNo',
            direction: 'ASC'
          }],
          fields: []
        }).then(response => {
          if (response.data.success === true && response.data.statusCode === 200) {
            this.sysCodeList = response.data.data.records
            if (this.sysCodeList.length === 0) {
              this.sysCodeName = '无效码表类型'
            } else {
              this.sysCodeName = this.sysCodeList[0].codeTypeName
            }
          } else {
            this.sysCodeName = '获取码表异常'
          }
        })
      }
    },
    watch: {
      value(val, oldVal) {
        this.init()
      },
      sysCodeValue(val, oldVal) {
        this.$emit('input', this.sysCodeValue)
      },
      sysCodeValueList(val, oldVal) {
        this.$emit('input', this.sysCodeValueList)
      }
    },
    data() {
      return {
        multiple: false,
        sysCodeName: '',
        sysCodeValue: '',
        sysCodeValueList: [],
        sysCodeList: []
      }
    },
    mounted() {
      this.init()
    }
  }

</script>

<style>
</style>
